<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/base_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Crafted_Item.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Material_Item.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Item_Family.php');


class item_DA extends base_DA {

    /********** Material Item Fields Name ************/
    const FL_MI_ID = 'id';
    const FL_MI_CODE = 'code';
    const FL_MI_GUILD_ID = 'guild_id';
    const FL_MI_NAME = 'name';
    const FL_MI_QUANTITY = 'quantity';
    const FL_MI_QUALITY = 'quality';

    /********** Crafted Item Fields Name ************/
    const FL_CI_ID = 'id';
    const FL_CI_CODE = 'code';
    const FL_CI_GUILD_ID = 'guild_id';
    const FL_CI_NAME = 'name';
    const FL_CI_QUANTITY = 'quantity';
    const FL_CI_QUALITY = 'quality';
    const FL_CI_COLOR = 'color';
    const FL_CI_ITEM_CLASS = 'item_class';
    const FL_CI_HP_BONUS = 'hp_bonus';
    const FL_CI_SAP_BONUS = 'sap_bonus';
    const FL_CI_STAM_BONUS = 'stam_bonus';
    const FL_CI_FOCUS_BONUS = 'focus_bonus';
    const FL_CI_HEAL_POINT = 'healpoint';
    const FL_CI_DURABILITY = 'durability';
    const FL_CI_SAPLOAD = 'sapload';
    const FL_CI_CURRENT_SAPLOAD = 'current_sapload';
    const FL_CI_ENCHANT = 'enchant';
    const FL_CI_HIT_RATE = 'hit_rate';
    const FL_CI_RANGE = 'item_range';
    const FL_CI_DESTROYED = 'destroyed';
    const FL_CI_DODGE_MODIFIER = 'dodge_modifier';
    const FL_CI_PARRY_MODIFIER = 'parry_modifier';
    const FL_CI_ADV_DODGE_MODIFIER = 'adv_dodge_modifier';
    const FL_CI_ADV_PARRY_MODIFIER = 'adv_parry_modifier';
    const FL_CI_PROTEC_FACTOR = 'protec_factor';
    const FL_CI_MAX_SLASH_PROTEC = 'max_slash_protec';
    const FL_CI_MAX_PIERCE_PROTEC = 'max_pierce_protec';
    const FL_CI_MAX_BLUNT_PROTEC = 'max_blunt_protec';

    /********** Item Code Fields Name ************/
    const FL_CO_FL_CODE = 'code';
    const FL_CO_FL_LABEL = 'label';
    const FL_CO_FL_FAMILY_ID = 'family_id';
    
    /********** Item Family Fields Name ************/
    const FL_FAM_ID = 'id';
    const FL_FAM_LABEL = 'label';
    const FL_FAM_ICON = 'icon';

    /********** Item Family Type Fields Name ************/
    const FL_FAMTYPE_ID = 'id';
    const FL_FAMTYPE_LABEL = 'label';
    const FL_FAMTYPE_ICON = 'icon';

    /********** '.$this->get_table_prefix().'_item_family_type_family Fields Name ************/
    const FL_FAMTYPE_FAM_FAMILY_ID = 'family_id';
    const FL_FAMTYPE_FAM_TYPE_ID = 'type_id';

    /********** Global Item Data Access Functions ************/
    /**
     * (PHP 5)<br/>
     * Insert an item
     * @param Item_Base $item 
     */
    public function insert_item(Item_Base $item) {
        if ($item->get_item_type_id() == Enum_Item_Type::CRAFTED_ITEM_TYPE_ID){
            $this->insert_crafted_item($item);
        }
        else {
            $this->insert_material_item($item);
        }
    }

    /**
     * (PHP 5)<br/>
     * Delete an item
     * @param int $id
     * @param int $item_type
     */
    public function delete_item($id, $item_type) {
        if ($item_type == Enum_Item_Type::CRAFTED_ITEM_TYPE_ID){
            $this->delete_crafted_item($id);
        }
        else {
            $this->delete_material_item($id);
        }
    }

    /**
     * (PHP 5)<br/>
     * Update item quantity
     * @param int $id
     * @param int $quantity
     * @param int $item_type
     */
    public function update_item_quantity($id, $quantity, $item_type){
        if ($item_type == Enum_Item_Type::CRAFTED_ITEM_TYPE_ID){
            $this->update_crafted_item_quantity($id, $quantity);
        }
        else {
            $this->update_material_item_quantity($id, $quantity);
        }
    }

    /********** Material Item Data Access Functions ************/

    /**
     * (PHP 5)<br/>
     * Get all Material_Item of a guild
     * @param int $guild_id
     * @return array of Material_Item
     */
    public function get_guild_material_item($guild_id){


        if ($guild_id == -1) {
            $sql_cmd = 'SELECT mi.*, g.'.guild_DA::FL_NAME.' as guild_name
                        FROM '.$this->get_table_prefix().'_material_item mi
                        INNER JOIN '.$this->get_table_prefix().'_guild g
                        ON g.'.guild_DA::FL_ID.' = mi.'.item_DA::FL_MI_GUILD_ID;
        }
        else {
            $sql_cmd = 'SELECT *
                        FROM '.$this->get_table_prefix().'_material_item
                        WHERE '.item_DA::FL_MI_GUILD_ID.' = '.$guild_id;
        }

        $sql_cmd .= ' ORDER BY '.item_DA::FL_MI_QUALITY.' DESC, '.item_DA::FL_MI_CODE;


//        echo '<br>item_DA->get_guild_material_item : '.$sql_cmd;

        $item_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($item_array); $i++) {
            $item = $item_array[$i];

            $guild_name = '';
            if (isset($item['guild_name'])){
                $guild_name = $item['guild_name'];
            }



            $result[] = new Material_Item($guild_id, $item[item_DA::FL_MI_ID], $item[item_DA::FL_MI_CODE],
                    $item[item_DA::FL_MI_NAME], $item[item_DA::FL_MI_QUALITY], $item[item_DA::FL_MI_QUANTITY],
                    substr($item[item_DA::FL_MI_CODE], 9, 3),$guild_name);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Insert a Material_Item
     * @param Material_Item $item
     */
    private function insert_material_item(Material_Item $item){
        $sql_cmd = 'INSERT INTO '.$this->get_table_prefix().'_material_item ('.item_DA::FL_MI_CODE.', '.item_DA::FL_MI_GUILD_ID.', '.item_DA::FL_MI_NAME
                        .', '.item_DA::FL_MI_QUANTITY.', '.item_DA::FL_MI_QUALITY.')
                    VALUES ('.$this->format_string_to_sql_param($item->get_code()).', '.$item->get_guild_id().', '.$this->format_string_to_sql_param($item->get_name()).',
                        '.$item->get_quantity().', '.$item->get_quality().')';

        //echo '<br>item_DA->insert_material_item : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, true);
    }

    /**
     * (PHP 5)<br/>
     * Delete a Material_Item
     * @param int $id
     */
    private function delete_material_item($id){
        $sql_cmd = 'DELETE FROM '.$this->get_table_prefix().'_material_item WHERE '.item_DA::FL_MI_ID.' = '.$id;

//        echo '<br>item_DA->delete_material_item : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Update Material_Item quantity
     * @param int $id
     * @param int $quantity
     */
    private function update_material_item_quantity($id, $quantity){
        $sql_cmd = 'UPDATE '.$this->get_table_prefix().'_material_item
                    SET '.item_DA::FL_MI_QUANTITY.' = '.$quantity
                    .' WHERE '.item_DA::FL_MI_ID.' = '.$id;

//        echo '<br>item_DA->update_material_item_quantity : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Search Material_Item in DB
     * @param int $type
     * @param int $family
     * @param string $code
     * @param int $quality
     * @param string $quality_com_signe must be egal to '=', '<', '<=', '>' or '>='
     */
    public function search_item($type, $family, $code, $quality, $quality_com_signe){

        //family = All Family
        if ($family == 40){
            $family = null;
        }


        $sql_cmd = 'SELECT DISTINCT mi.*, g.'.guild_DA::FL_NAME.' as guild_name FROM '.$this->get_table_prefix().'_material_item mi ';
        $sql_cmd .= ' INNER JOIN ( ';
        $sql_cmd .= ' SELECT CONCAT( c.'.item_DA::FL_CO_FL_CODE.', \'%\') AS code ';
        $sql_cmd .= ' FROM '.$this->get_table_prefix().'_item_family_type_family famtype_fam, '.$this->get_table_prefix().'_item_code c ';
        $sql_cmd .= ' WHERE c.'.item_DA::FL_CO_FL_FAMILY_ID.' = famtype_fam.'.item_DA::FL_FAMTYPE_FAM_FAMILY_ID.' ';

        if (isset($type))   $sql_cmd .= ' AND famtype_fam.'.item_DA::FL_FAMTYPE_FAM_TYPE_ID.' = '.$type.' ';
        if (isset($family)) $sql_cmd .= ' AND famtype_fam.'.item_DA::FL_FAMTYPE_FAM_FAMILY_ID.' = '.$family.' ';
        if (isset($code))   $sql_cmd .= ' AND c.'.item_DA::FL_CO_FL_CODE.' LIKE CONCAT("'.$code.'" , "%") ';

        $sql_cmd .= ' ) Q ON mi.'.item_DA::FL_MI_CODE.' LIKE Q.code ';

        if ((isset($quality)) && (isset($quality_com_signe))){
            $sql_cmd .= ' AND mi.'.item_DA::FL_MI_QUALITY.' '.$quality_com_signe.' '.$quality.' ';
        }

        $sql_cmd .= ' INNER JOIN '.$this->get_table_prefix().'_guild g ';
        $sql_cmd .= ' ON g.'.guild_DA::FL_ID.' = mi.'.item_DA::FL_MI_GUILD_ID.' ';
        $sql_cmd .= ' ORDER BY mi.'.item_DA::FL_MI_QUALITY.' DESC, mi.'.item_DA::FL_MI_CODE;
        

//        echo '<br>item_DA->search_item : '.$sql_cmd;

        $item_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($item_array); $i++) {
            $item = $item_array[$i];

            $guild_name = '';
            if (isset($item['guild_name'])){
                $guild_name = $item['guild_name'];
            }

            $result[] = new Material_Item(-1, $item[item_DA::FL_MI_ID], $item[item_DA::FL_MI_CODE],
                    $item[item_DA::FL_MI_NAME], $item[item_DA::FL_MI_QUALITY], $item[item_DA::FL_MI_QUANTITY],
                    substr($item[item_DA::FL_MI_CODE], 9, 3),$guild_name);
        }

        return (isset($result) ? $result : null);
    }
    
    /********** Crafted Item Data Access Functions ************/
    
    /**
     * (PHP 5)<br/>
     * Get all Crafted_Item of a guild
     * @param int $guild_id -1 allow to get the content of all room in DB
     * @return array of Crafted_Item
     */
    public function get_guild_crafted_item($guild_id) {

        if ($guild_id == -1) {
            $sql_cmd = 'SELECT ci.*, g.'.guild_DA::FL_NAME.' as guild_name
                         FROM '.$this->get_table_prefix().'_crafted_item ci
                         INNER JOIN '.$this->get_table_prefix().'_guild g
                         ON g.'.guild_DA::FL_ID.' = ci.'.item_DA::FL_CI_GUILD_ID;

            
        }
        else {
            $sql_cmd = 'SELECT *
                         FROM '.$this->get_table_prefix().'_crafted_item
                         WHERE '.item_DA::FL_CI_GUILD_ID.' = '.$guild_id;
        }

        $sql_cmd .= ' ORDER BY '.item_DA::FL_CI_QUALITY.' DESC, '.item_DA::FL_CI_CODE;

//        echo '<br>item_DA->get_guild_crafted_item : '.$sql_cmd;

        $item_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($item_array); $i++) {
            $item = $item_array[$i];

            $guild_name = '';
            if (isset($item['guild_name'])){
                $guild_name = $item['guild_name'];
            }

            $skin = 1;
            if (strpos($item[item_DA::FL_CI_CODE],'_2.') == true) $skin = 2;
            if (strpos($item[item_DA::FL_CI_CODE],'_3.') == true) $skin = 3;

            $result[] = new Crafted_Item($guild_id, $item[item_DA::FL_CI_ID], $item[item_DA::FL_CI_CODE], $item[item_DA::FL_CI_NAME], $item[item_DA::FL_CI_QUALITY], $item[item_DA::FL_CI_QUANTITY],
                    $item[item_DA::FL_CI_ITEM_CLASS],  $skin, $item[item_DA::FL_CI_COLOR], $item[item_DA::FL_CI_HEAL_POINT], $item[item_DA::FL_CI_DURABILITY],
                    $item[item_DA::FL_CI_SAPLOAD], $item[item_DA::FL_CI_CURRENT_SAPLOAD], $item[item_DA::FL_CI_ENCHANT], $item[item_DA::FL_CI_HIT_RATE], $item[item_DA::FL_CI_RANGE],
                    $item[item_DA::FL_CI_DODGE_MODIFIER], $item[item_DA::FL_CI_PARRY_MODIFIER], $item[item_DA::FL_CI_ADV_DODGE_MODIFIER], $item[item_DA::FL_CI_ADV_PARRY_MODIFIER], $item[item_DA::FL_CI_PROTEC_FACTOR],
                    $item[item_DA::FL_CI_MAX_SLASH_PROTEC], $item[item_DA::FL_CI_MAX_PIERCE_PROTEC], $item[item_DA::FL_CI_MAX_BLUNT_PROTEC], $item[item_DA::FL_CI_DESTROYED],
                    $item[item_DA::FL_CI_HP_BONUS], $item[item_DA::FL_CI_SAP_BONUS], $item[item_DA::FL_CI_STAM_BONUS], $item[item_DA::FL_CI_FOCUS_BONUS],$guild_name);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Insert a Crafted_Item
     * @param Crafted_Item $item
     */
    private function insert_crafted_item(Crafted_Item $item){

        $sql_cmd = 'INSERT INTO '.$this->get_table_prefix().'_crafted_item ('.item_DA::FL_CI_GUILD_ID.','.item_DA::FL_CI_CODE.','.item_DA::FL_CI_NAME.','.item_DA::FL_CI_QUALITY.','.item_DA::FL_CI_QUANTITY.'
                            ,'.item_DA::FL_CI_ITEM_CLASS.','.item_DA::FL_CI_COLOR.','.item_DA::FL_CI_HEAL_POINT.','.item_DA::FL_CI_DURABILITY.'
                            ,'.item_DA::FL_CI_SAPLOAD.','.item_DA::FL_CI_CURRENT_SAPLOAD.','.item_DA::FL_CI_ENCHANT.','.item_DA::FL_CI_HIT_RATE.','.item_DA::FL_CI_RANGE.'
                            ,'.item_DA::FL_CI_DODGE_MODIFIER.','.item_DA::FL_CI_PARRY_MODIFIER.','.item_DA::FL_CI_ADV_DODGE_MODIFIER.','.item_DA::FL_CI_ADV_PARRY_MODIFIER.','.item_DA::FL_CI_PROTEC_FACTOR.'
                            ,'.item_DA::FL_CI_MAX_SLASH_PROTEC.','.item_DA::FL_CI_MAX_PIERCE_PROTEC.','.item_DA::FL_CI_MAX_BLUNT_PROTEC.','.item_DA::FL_CI_DESTROYED.'
                            ,'.item_DA::FL_CI_HP_BONUS.','.item_DA::FL_CI_SAP_BONUS.','.item_DA::FL_CI_STAM_BONUS.','.item_DA::FL_CI_FOCUS_BONUS.')

                        VALUES ('.$item->get_guild_id().','.$this->format_string_to_sql_param($item->get_code()).','.$this->format_string_to_sql_param($item->get_name()).','.$item->get_quality().','.$item->get_quantity().'
                            ,'.$item->get_item_class_id().','.$this->format_null_value($item->get_color()).','.$this->format_null_value($item->get_healpoint()).','.$this->format_null_value($item->get_durability()).'
                            ,'.$this->format_null_value($item->get_sapload()).','.$this->format_null_value($item->get_current_sapload()).','.$this->format_null_value($item->get_enchant()).'
                            ,'.$this->format_null_value($item->get_hit_rate()).','.$this->format_null_value($item->get_range()).'
                            ,'.$this->format_null_value($item->get_dodge_modif()).','.$this->format_null_value($item->get_parry_modif()).','.$this->format_null_value($item->get_adv_dodge_modif()).'
                            ,'.$this->format_null_value($item->get_adv_parry_modif()).','.$this->format_null_value($item->get_prot_factor()).'
                            ,'.$this->format_null_value($item->get_max_slash_prot()).','.$this->format_null_value($item->get_max_pierce_prot()).','.$this->format_null_value($item->get_max_blunt_prot()).'
                            ,'.$this->format_null_value($item->get_destroyed()).','.$this->format_null_value($item->get_healpoint_bonus()).','.$this->format_null_value($item->get_sap_bonus()).'
                            ,'.$this->format_null_value($item->get_stam_bonus()).','.$this->format_null_value($item->get_focus_bonus()).')';


//        echo '<br>item_DA->insert_crafted_item : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, true);
    }

    /**
     * (PHP 5)<br/>
     * Delete a Crafted_Item
     * @param int $id
     */
    private function delete_crafted_item($id){
        $sql_cmd = 'DELETE FROM '.$this->get_table_prefix().'_crafted_item WHERE '.item_DA::FL_CI_ID.' = '.$id;

//        echo '<br>item_DA->delete_crafted_item : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }

    /**
     * (PHP 5)<br/>
     * Update Crafted_Item quantity
     * @param int $id
     * @param int $quantity
     */
    private function update_crafted_item_quantity($id, $quantity){
        $sql_cmd = 'UPDATE '.$this->get_table_prefix().'_crafted_item
                    SET '.item_DA::FL_CI_QUANTITY.' = '.$quantity
                    .' WHERE '.item_DA::FL_CI_ID.' = '.$id;

//        echo '<br>item_DA->update_crafted_item_quantity : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, false);
    }


    /********** Family Type Data Access Functions ************/

    /**
     * (PHP 5)<br/>
     * Get item family type
     * @return Item_Family_Type
     */
    public function get_family_type(){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_item_family_type ORDER BY '.item_DA::FL_FAMTYPE_LABEL;

//        echo '<br>item_DA->get_family_type : '.$sql_cmd;

        $type_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($type_array); $i++) {
            $type = $type_array[$i];

            $result[] = new Item_Family_Type($type[item_DA::FL_FAMTYPE_ID], $type[item_DA::FL_FAMTYPE_LABEL], $type[item_DA::FL_FAMTYPE_ICON]);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Get item family type
     * @param int $type_id
     * @return Item_Family_Type
     */
    public function get_family_type_by_id($type_id){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_item_family_type WHERE '.item_DA::FL_FAMTYPE_ID.' = '.$type_id;
//        echo '<br>item_DA->get_family_type_by_id : '.$sql_cmd;

        $type_array = $this->execute_sql_command($sql_cmd, false);

        if ((isset($type_array)) && (count($type_array)> 0)){
            $result = new Item_Family_Type($type_array[0][item_DA::FL_FAMTYPE_ID], $type_array[0][item_DA::FL_FAMTYPE_LABEL],
                    $type_array[0][item_DA::FL_FAMTYPE_ICON]);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Get item family
     * @param int $family_id
     * @return Item_Family
     */
    public function get_family_by_id($family_id){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_item_family WHERE '.item_DA::FL_FAM_ID.' = '.$family_id;
//        echo '<br>item_DA->get_family_by_id : '.$sql_cmd;

        $family_array = $this->execute_sql_command($sql_cmd, false);

        if ((isset($family_array)) && (count($family_array) > 0)){
            $result = new Item_Family($family_array[0][item_DA::FL_FAM_ID], $family_array[0][item_DA::FL_FAM_LABEL],
                    $family_array[0][item_DA::FL_FAM_ICON]);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Get item family
     * @param int $type_id
     * @return array of Item_Family
     */
    public function get_family($type_id){
        $sql_cmd = 'SELECT * 
                     FROM '.$this->get_table_prefix().'_item_family f ';

        if ((isset($type_id)) && ($type_id != -1)){
            $sql_cmd .= ' INNER JOIN '.$this->get_table_prefix().'_item_family_type_family type_fam ON type_fam.'.item_DA::FL_FAMTYPE_FAM_FAMILY_ID.' = f.'.item_DA::FL_FAM_ID.'
                            AND (type_fam.'.item_DA::FL_FAMTYPE_FAM_TYPE_ID.' = '.$type_id. ' OR type_fam.'.item_DA::FL_FAMTYPE_FAM_FAMILY_ID.' = 40) ';

        }

        $sql_cmd .= ' ORDER BY f.'.item_DA::FL_FAM_LABEL;


//        echo '<br>item_DA->get_family : '.$sql_cmd;

        $fam_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($fam_array); $i++) {
            $fam = $fam_array[$i];

            $result[] = new Item_Family($fam[item_DA::FL_FAM_ID], $fam[item_DA::FL_FAM_LABEL], $fam[item_DA::FL_FAM_ICON]);
        }
        
        return (isset($result) ? $result : null);
    }

    /**
     * Get item code
     * @param string $code
     * @return Item_Code
     */
    public function get_item_code_by_id($code){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_item_code WHERE '.item_DA::FL_CO_FL_CODE.' = '.$this->format_string_to_sql_param($code) ;
//        echo '<br>item_DA->get_item_code_by_id : '.$sql_cmd;

        $code_array = $this->execute_sql_command($sql_cmd, false);

        if ((isset($code_array)) && (count($code_array) > 0)){
            $result = new Item_Code($code_array[0][item_DA::FL_CO_FL_CODE], $code_array[0][item_DA::FL_CO_FL_LABEL]);
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Get item code
     * @param int $family_id
     * @return Item_Code
     */
    public function get_item_code($family_id){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_item_code ';

        if (isset($family_id)){
            $sql_cmd .= ' WHERE family_id = '.$family_id;
            $sql_cmd .= ' OR family_id = 40'; //select all code family
        }

        $sql_cmd .= ' ORDER BY '.  item_DA::FL_CO_FL_LABEL;
        
//        echo '<br>item_DA->get_item_code : '.$sql_cmd;

        $code_array = $this->execute_sql_command($sql_cmd, false);

        for ($i = 0; $i < count($code_array); $i++) {
            $code = $code_array[$i];

            $result[] = new Item_Code($code[item_DA::FL_CO_FL_CODE], $code[item_DA::FL_CO_FL_LABEL]);
        }

        return (isset($result) ? $result : null);

    }

}
?>
